<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Java 内存操作流详解</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        .card-hover {
            transition: all 0.3s ease;
        }
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 40px rgba(0,0,0,0.1);
        }
        .code-block {
            background: #282c34;
            color: #abb2bf;
            border-radius: 12px;
            overflow: hidden;
        }
        .table-custom {
            background: white;
            border-radius: 12px;
            overflow: hidden;
            box-shadow: 0 4px 6px rgba(0,0,0,0.07);
        }
        .table-custom th {
            background: #f8f9fa;
            font-weight: 600;
            color: #2d3748;
        }
        .table-custom td {
            border-bottom: 1px solid #e2e8f0;
        }
        .table-custom tr:last-child td {
            border-bottom: none;
        }
        .drop-cap {
            float: left;
            font-size: 4rem;
            line-height: 1;
            font-weight: 700;
            margin-right: 0.5rem;
            color: #667eea;
            font-family: 'Noto Serif SC', serif;
        }
        .section-divider {
            height: 2px;
            background: linear-gradient(to right, transparent, #cbd5e0, transparent);
            margin: 3rem 0;
        }
        .mermaid {
            display: flex;
            justify-content: center;
            margin: 2rem 0;
        }
        .highlight-box {
            background: linear-gradient(135deg, #ffeaa7 0%, #fab1a0 100%);
            padding: 1.5rem;
            border-radius: 12px;
            position: relative;
            overflow: hidden;
        }
        .highlight-box::before {
            content: '';
            position: absolute;
            top: -50%;
            right: -50%;
            width: 200%;
            height: 200%;
            background: radial-gradient(circle, rgba(255,255,255,0.3) 0%, transparent 70%);
            animation: shimmer 3s infinite;
        }
        @keyframes shimmer {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <section class="hero-gradient text-white py-20 px-6">
        <div class="max-w-6xl mx-auto text-center">
            <h1 class="text-5xl md:text-6xl font-bold mb-6 animate-pulse">
                <i class="fas fa-memory mr-4"></i>Java 内存操作流
            </h1>
            <p class="text-xl md:text-2xl opacity-90 max-w-3xl mx-auto">
                探索 ByteArrayInputStream 与 ByteArrayOutputStream 的强大功能，掌握内存中数据流的高效处理技巧
            </p>
        </div>
    </section>

    <!-- Main Content -->
    <main class="max-w-6xl mx-auto px-6 py-12">
        <!-- Introduction Card -->
        <div class="bg-white rounded-2xl shadow-xl p-8 mb-12 card-hover">
            <h2 class="text-3xl font-bold text-gray-800 mb-6 flex items-center">
                <i class="fas fa-lightbulb text-yellow-500 mr-3"></i>
                为什么选择内存操作流？
            </h2>
            <p class="text-lg text-gray-700 leading-relaxed">
                <span class="drop-cap">内</span>存操作流是 Java I/O 体系中的重要组成部分，它允许我们在内存中直接进行数据的读写操作，无需创建实际的文件。这种方式特别适合处理临时数据、测试场景以及需要高性能数据处理的场合。相比传统的文件 I/O，内存操作流具有更快的访问速度和更灵活的使用方式。
            </p>
        </div>

        <!-- Mermaid Diagram -->
        <div class="bg-white rounded-2xl shadow-xl p-8 mb-12">
            <h2 class="text-3xl font-bold text-gray-800 mb-6 flex items-center">
                <i class="fas fa-project-diagram text-purple-500 mr-3"></i>
                内存操作流架构图
            </h2>
            <div class="mermaid">
                graph TB
                    A[Java I/O 流体系] --> B[字节流]
                    A --> C[字符流]
                    B --> D[输入流]
                    B --> E[输出流]
                    D --> F[ByteArrayInputStream]
                    E --> G[ByteArrayOutputStream]
                    F --> H[从内存读取数据]
                    G --> I[向内存写入数据]
                    H --> J[byte数组作为数据源]
                    I --> K[内部byte数组存储]
                    
                    style A fill:#667eea,stroke:#fff,stroke-width:2px,color:#fff
                    style F fill:#48bb78,stroke:#fff,stroke-width:2px,color:#fff
                    style G fill:#ed8936,stroke:#fff,stroke-width:2px,color:#fff
            </div>
        </div>

        <!-- ByteArrayInputStream Section -->
        <div class="mb-12">
            <h2 class="text-3xl font-bold text-gray-800 mb-8 flex items-center">
                <i class="fas fa-download text-green-500 mr-3"></i>
                ByteArrayInputStream 详解
            </h2>
            
            <div class="table-custom">
                <table class="w-full">
                    <thead>
                        <tr>
                            <th class="px-6 py-4 text-left">序号</th>
                            <th class="px-6 py-4 text-left">方法</th>
                            <th class="px-6 py-4 text-left">类型</th>
                            <th class="px-6 py-4 text-left">描述</th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr class="hover:bg-gray-50 transition-colors">
                            <td class="px-6 py-4">
                                <span class="inline-flex items-center justify-center w-8 h-8 bg-green-100 text-green-800 rounded-full font-semibold">1</span>
                            </td>
                            <td class="px-6 py-4 font-mono text-sm">public ByteArrayInputStream(byte[] buf)</td>
                            <td class="px-6 py-4">
                                <span class="px-3 py-1 bg-blue-100 text-blue-800 rounded-full text-sm">构造</span>
                            </td>
                            <td class="px-6 py-4">将全部内容写入内存中</td>
                        </tr>
                        <tr class="hover:bg-gray-50 transition-colors">
                            <td class="px-6 py-4">
                                <span class="inline-flex items-center justify-center w-8 h-8 bg-green-100 text-green-800 rounded-full font-semibold">2</span>
                            </td>
                            <td class="px-6 py-4 font-mono text-sm">public ByteArrayInputStream(byte[] buf, int offset, int length)</td>
                            <td class="px-6 py-4">
                                <span class="px-3 py-1 bg-blue-100 text-blue-800 rounded-full text-sm">构造</span>
                            </td>
                            <td class="px-6 py-4">将指定范围的内容写入到内存中</td>
                        </tr>
                    </tbody>
                </table>
            </div>
        </div>

        <div class="section-divider"></div>

        <!-- ByteArrayOutputStream Section -->
        <div class="mb-12">
            <h2 class="text-3xl font-bold text-gray-800 mb-8 flex items-center">
                <i class="fas fa-upload text-orange-500 mr-3"></i>
                ByteArrayOutputStream 详解
            </h2>
            
            <div class="table-custom">
                <table class="w-full">
                    <thead>
                        <tr>
                            <th class="px-6 py-4 text-left">序号</th>
                            <th class="px-6 py-4 text-left">方法</th>
                            <th class="px-6 py-4 text-left">类型</th>
                            <th class="px-6 py-4 text-left">描述</th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr class="hover:bg-gray-50 transition-colors">
                            <td class="px-6 py-4">
                                <span class="inline-flex items-center justify-center w-8 h-8 bg-orange-100 text-orange-800 rounded-full font-semibold">1</span>
                            </td>
                            <td class="px-6 py-4 font-mono text-sm">public ByteArrayOutputStream()</td>
                            <td class="px-6 py-4">
                                <span class="px-3 py-1 bg-blue-100 text-blue-800 rounded-full text-sm">构造</span>
                            </td>
                            <td class="px-6 py-4">创建对象</td>
                        </tr>
                        <tr class="hover:bg-gray-50 transition-colors">
                            <td class="px-6 py-4">
                                <span class="inline-flex items-center justify-center w-8 h-8 bg-orange-100 text-orange-800 rounded-full font-semibold">2</span>
                            </td>
                            <td class="px-6 py-4 font-mono text-sm">public void write(int b)</td>
                            <td class="px-6 py-4">
                                <span class="px-3 py-1 bg-green-100 text-green-800 rounded-full text-sm">方法</span>
                            </td>
                            <td class="px-6 py-4">将内容从内存中输出</td>
                        </tr>
                    </tbody>
                </table>
            </div>
        </div>

        <div class="section-divider"></div>

        <!-- Code Example Section -->
        <div class="mb-12">
            <h2 class="text-3xl font-bold text-gray-800 mb-8 flex items-center">
                <i class="fas fa-code text-indigo-500 mr-3"></i>
                实战代码示例
            </h2>
            
            <div class="code-block p-6">
                <div class="flex items-center justify-between mb-4">
                    <span class="text-gray-400 text-sm">Java</span>
                    <button class="text-gray-400 hover:text-white transition-colors">
                        <i class="fas fa-copy"></i>
                    </button>
                </div>
                <pre class="text-sm leading-relaxed"><code><span class="text-purple-400">public class</span> <span class="text-yellow-300">Test01</span> {
    <span class="text-purple-400">public static void</span> <span class="text-blue-300">main</span>(<span class="text-orange-300">String</span>[] args) <span class="text-purple-400">throws</span> <span class="text-orange-300">Exception</span> {
        <span class="text-orange-300">String</span> str = <span class="text-green-300">"hello World"</span>;
        
        <span class="text-gray-500">// 从内存中读取数据</span>
        <span class="text-orange-300">ByteArrayInputStream</span> bis = <span class="text-purple-400">new</span> <span class="text-orange-300">ByteArrayInputStream</span>(str.<span class="text-blue-300">getBytes</span>());
        <span class="text-gray-500">// 写到内存中</span>
        <span class="text-orange-300">ByteArrayOutputStream</span> bos = <span class="text-purple-400">new</span> <span class="text-orange-300">ByteArrayOutputStream</span>();
        
        <span class="text-purple-400">byte</span>[] by = <span class="text-purple-400">new byte</span>[<span class="text-orange-300">1024</span>];
        <span class="text-purple-400">int</span> read = bis.<span class="text-blue-300">read</span>(by, <span class="text-orange-300">0</span>, by.length); <span class="text-gray-500">// 读到一个数组里</span>
        bos.<span class="text-blue-300">write</span>(by, <span class="text-orange-300">0</span>, read); <span class="text-gray-500">// 把数组写到内存中去</span>
        <span class="text-orange-300">System</span>.out.<span class="text-blue-300">println</span>(bos.<span class="text-blue-300">toString</span>()); <span class="text-gray-500">// 取出内存中的内容</span>
    }
}</code></pre>
            </div>
        </div>

        <!-- Best Practices -->
        <div class="highlight-box mb-12